<?php
/**
 * RuiNuoCode低代码后台系统 - 团队十多年低代码经验汇集巨献!
 * =========================================================
 * Copy right 2019-2029 张家界瑞诺科技有限公司, 保留所有权利。
 * ----------------------------------------------
 * 官方网址: https://www.ruinuocrm.com
 * =========================================================
 */
namespace app\<app>\service\<controller>;
use app\common\service\Validate as ValidateService;//表单数据验证服务
use app\common\service\Power as PowerService;//表单权限验证服务
use app\common\service\Logic as LogicService;//表单更新服务
use app\common\model\<mode> as DataMode;//当前模型
use app\<app>\service\<controller>\Custom as RuinuoCustom;//自定义方法
use think\facade\Cache;
class Logic extends RuinuoCustom
{

    /*修改->获取表单配置->过滤表单参数->验证表单->验证字段权限->修改完成*/
    public function Edit($params = [])
    {
        if (empty($params['id'])) {
            return ['code' => 203, 'msg' => '参数ID不能为空', 'data' => []];
        }
        $Model = new DataMode();
        $ValidateService = new ValidateService();
        $PowerService = new PowerService();
        $check = $ValidateService->Rule($this->GetVerify(),$params,$params['id']);
        if ($check['code'] == '203') {
            return $check;
        }
        $where="a.id='" . $params['id'] . "'";
        $where.=$PowerService->IndexWhere('<app>:<controller>');
        $find = $Model->alias('a')->where($where)->count();
        if ($find == 0) {
            return ['code' => 203, 'msg' => '无此数据或者无权限', 'data' => []];
        }
        $formData =$this->GetFormData($params);
        $ItemData=$PowerService->FormField($formData,'<app>:<controller>','edit');
        $ItemData = $this->savebefore($ItemData,$params['id']); //保存前处理
        if (!empty($ItemData['code'])) {
            return ['code' => 203, 'msg' => $ItemData['msg'],'data'=>[]];
        }
        $updata['values']=json_encode($ItemData['a'], JSON_UNESCAPED_UNICODE);
        $Model->where("`id`='" . $params['id'] . "'")->Update($updata);
        $back['id'] = $params['id'];
        $saveafter = $this->saveafter($back['id']);//保存后处理
        if (!empty($saveafter['code'])) {
            return ['code' => 203, 'msg' => $saveafter['msg'],'data'=>[]];
        }
        Cache::set('ruinuo_setting_code_<controller>', '', 600);
        return ['code' => 200, 'msg' => '修改成功', 'data' => $back];
    }


    /*查看->获取表单配置->过滤表单参数->验证字段查看权限->查看完成*/
    public function Detail($params = [])
    {
        $Model = new DataMode();
        $LogicService = new LogicService();
        $PowerService = new PowerService();
        $find = [];
        if (!empty($params['id'])) {
            $where="a.id='" . $params['id'] . "'";
            $where.=$PowerService->IndexWhere('<app>:<controller>');
            $find = $Model->alias('a')->where($where)->field("id,values")->findOrEmpty()->toArray();
            if (empty($find)) {
                return ['code' => 203, 'msg' => '无此数据或者无权限', 'data' => []];
            }
            $values=!empty($find['values']) ? json_decode($find['values'],true) : [];
            foreach ($values as $k => $v) {
                $find[$k]=$v;
            }
        }
        $node=!empty($params['node']) ? $params['node'] : 'detail';
        $power=$PowerService->FormField($find,'<app>:<controller>','detail',$node);//是否可见权限
        $data['detail']=$power['detail'];
        $data['power']=$power['power'];
        $source='<options_num>';
        $data['source'] = $LogicService->GetSource($source);
        $data = $this->oneafter($data);//查询后处理
        if (!empty($data['code'])) {
            return ['code' => 203, 'msg' => $data['msg'], 'data' => []];
        }
        return ['code' => 200, 'msg' => '请求成功', 'data' => $data];
    }



    /*获取表单参数*/
    public function GetFormData($params = [])
    {
        $LogicService = new LogicService();
        <FormData>
        return $data;
    }

    /*获取表单验证参数*/
    public function GetVerify()
    {
        <verify>
        return $data;
    }


}